<template>
  <div class="content">
    <div class="category">
      <div :class="{'category__item':true,'category__item--active':item.tab == currentTab}" v-for="item in categories"
           :key="item.name"
           @click="()=>handleTabClick(item.tab)"
      >
        {{ item.name }}
      </div>

    </div>
    <div class="product">
      <div class="product__item" v-for="item in list" :key="item._id">
        <img class="product__item__img" src="http://www.dell-lee.com/imgs/vue3/near.png">
        <div class="product__item__detail">
          <h4 class="product__item__title">{{ item.name }}</h4>
          <p class="product__item__sales">月售{{ item.sales }}件</p>
          <p class="product__item__price">
            <span class="product__item__yen">&yen;</span>{{ item.price }}
            <span class="product__item__origin">&yen;{{ item.oldPrice }}</span>
          </p>
        </div>
        <div class="product__number">
          <span class="product__number__minus">-</span>
          2
          <span class="product__number__plus">+</span>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import {get} from '../../utils/require.js';
import {reactive, ref, toRefs,watchEffect} from "vue";
import {useRoute} from "vue-router";

const categories = [{
  name: '全部商品',
  tab: 'all',
}, {
  name: '秒杀',
  tab: 'seckill',
}, {
  name: '新鲜水果',
  tab: 'fruit',
}];
//和tab切换相关的逻辑
const useTabEffecct = () => {
  const currentTab = ref(categories[0].tab)
  const handleTabClick = (tab) => {
    currentTab.value = tab
  }
  return {currentTab, handleTabClick}
}

const useCurrentListEffect = (currentTab)=>{
  const content = reactive({list:[]})
  const route = useRoute()
  const shop_id = route.params.shop
  const getContentData = async () => {
    const result = await get(`/shop_detail.php/{$shop_id}`, {
      tab:currentTab.value
    })
    if (result?.errno == 0 && result?.data) {
      content.list = result.data
    }
  }
  watchEffect(()=>{
    getContentData()
  })
  const {list} = toRefs(content)
  return {list}
}
export default {
  name: 'Content',
  setup() {
    const {currentTab, handleTabClick} = useTabEffecct()
    const {list} = useCurrentListEffect(currentTab)

    return {currentTab, categories, handleTabClick, list}
  }
}
</script>

<style lang="scss" scoped>
@use "@/style/viriables" as *;
@use "@/style/mixins" as *;

.content {
  display: flex;
  position: absolute;
  left: 0;
  right: 0;
  top: 1.5rem;
  bottom: .5rem;
}

.category {
  overflow-y: scroll;
  height: 100%;
  width: .76rem;
  background: $search-bgColor;

  &__item {
    line-height: .4rem;
    text-align: center;
    color: #333;
    font-size: .14rem;

    &--active {
      background: $bgColor;
    }
  }
}

.product {
  flex: 1;
  overflow-y: scroll;

  &__item {
    position: relative;
    display: flex;
    padding: .12rem 0;
    margin: 0 .15rem;
    border-bottom: .01rem solid $content-bgcolor;

    &__detail {

      overflow: hidden;
    }

    &__img {
      width: .68rem;
      height: .68rem;
      margin-right: .16rem;
    }

    &__title {
      line-height: .2rem;
      margin: 0;
      font-size: .14rem;
      color: #333;
      @include ellipsis;
    }

    &__sales {
      margin: .06rem 0;
      line-height: .16rem;
      font-size: .12rem;
      color: #333;
    }

    &__price {
      margin: .06rem 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $hightlight-fontColor;
    }

    &__yen {
      font-size: .12rem;
    }

    &__origin {
      margin-left: .06rem;
      line-height: .2rem;
      font-size: .12rem;
      color: $light-fontColor;
      text-decoration: line-through;
    }

    .product__number {
      position: absolute;
      right: 0;
      bottom: 0;

      &__minus, &__plus {
        display: inline-block;
        width: .2rem;
        height: .2rem;
        line-height: .15rem;
        border-radius: 50%;
        text-align: center;
        font-size: .2rem;
      }

      &__minus {
        border: .01rem solid $medium-fontColor;
        color: $medium-fontColor;
        margin-right: .1rem;
      }

      &__plus {
        background: $btn-bgColor;
        color: $bgColor;
        margin-left: .1rem;
      }
    }
  }
}
</style>